package cn.lu.controller;

import cn.lu.pojo.Chapter;
import cn.lu.pojo.Section;
import cn.lu.service.SectionService;
import cn.lu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 小节
 */
@Controller
@RequestMapping("/section")
@ResponseBody
public class SectionController {
    @Autowired
    private SectionService sectionService;

    @RequestMapping(value = "/admin",method = RequestMethod.POST)
    public Result addInsert(@RequestBody Section section) {
        return new Result().success(sectionService.addInsert(section));
    }

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result selectChapterId(@RequestParam(value = "chapterId")Integer chapterId) {
        return new Result().success(sectionService.selectChapterId(chapterId));
    }

    @RequestMapping(value = "",method = RequestMethod.DELETE)
    public Result sectionId(@RequestParam(value = "sectionId")Integer sectionId, HttpServletRequest request) {
        return new Result().success(sectionService.deleteByIdAndUserId(sectionId,Integer.parseInt(request.getHeader("userId"))));
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result addInTheUserId(@RequestBody Section section, HttpServletRequest request){
        return sectionService.addInTheUserId(section,Integer.parseInt(request.getHeader("userId")));
    }

    @RequestMapping(value = "",method = RequestMethod.PUT)
    public Result updateById(@RequestBody  Section section,HttpServletRequest request) {
        return new Result().success(sectionService.updateByIdAndUserId(section,Integer.parseInt(request.getHeader("userId"))));
    }
}